/*******************************************************************************
 * @project: ServletTest
 * @package: com.burns.servlet
 * @file: HelloWorld.java
 * @author: Administrator
 * @created: 2017-5-16
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.servlet;

//导入必需的 java 库
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.*;
import javax.servlet.http.*;

import com.burns.servlet.util.GetDocumentByUrl;

import net.sf.json.JSONObject;

//扩展 HttpServlet 类
public class GetCode extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String message;

	public void init() throws ServletException {
		// 执行必需的初始化
		message = "Hello World";
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		// String code = (String) request.getAttribute("code");
		String code = request.getParameter("code");

		String url = "http://api.csdn.net/oauth2/access_token?client_id=1100309&client_secret=23eaf6637b2d43128c518b8d7e4078ac&grant_type=authorization_code&redirect_uri=http://localhost:8080/ServletTest/get_access_token&code="
				+ code;

		// -------------------------------
		// 返回结果集
		String document = null;
		try {
			document = GetDocumentByUrl.getStringByUrl(url);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("document>>>" + document);
		JSONObject json = JSONObject.fromObject(document.toString());
		// 查看json格式
		// System.out.println(json.toString());
		// 获取json中某个对象
		// JSONObject obj = (JSONObject) json.get("weatherinfo");
		// System.out.println(obj.toString());
		// 获取对象中某个属性的值
		String access_token = json.getString("access_token");
		System.out.println("access_token>>>" + access_token);
		// -------------------------------
		// 1、获取用户信息
		String url2 = "http://api.csdn.net/blog/getinfo?access_token="
				+ access_token;
		String userInfo = null;
		try {
			userInfo = GetDocumentByUrl.getStringByUrl(url2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("userInfo>>>" + userInfo);
		// -----------------------
		// 2、获取博主的统计信息
		// -----------------------
		// 3、获取博主的勋章
		String url3 = "http://api.csdn.net/blog/getmedal?access_token="
				+ access_token;
		String medal = null;
		try {
			medal = GetDocumentByUrl.getStringByUrl(url3);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("获取博主的勋章>>>" + medal);

		// -----------------------
		// 4、获取博主的专栏
		String columnUrl = "http://api.csdn.net/blog/getcolumn?access_token="
				+ access_token;
		String column = null;
		try {
			column = GetDocumentByUrl.getStringByUrl(columnUrl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("获取博主的专栏>>>" + column);

		// -----------------------
		// 5、获取博主的文章列表
		String articlelistUrl = "http://api.csdn.net/blog/getarticlelist?access_token="
				+ access_token;
		String articlelist = null;
		try {
			articlelist = GetDocumentByUrl.getStringByUrl(articlelistUrl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("获取博主的文章列表>>>" + articlelist);

		// 设置响应内容类型
		response.setHeader("content-type", "text/html;charset=UTF-8");
		// -----------------------
		
		// 6、发表/修改文章
		String title="测试标题";
		String content="测试正文。。。。。。。。。";
		String savearticleUrl = "http://api.csdn.net/blog/savearticle?access_token="
				+ access_token+"&title="+title+"&content="+content;
		String savearticle = null;
		try {
			savearticle = GetDocumentByUrl.getStringByUrl(savearticleUrl);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("发表/修改文章>>>" + savearticle);

		// 设置响应内容类型
		response.setHeader("content-type", "text/html;charset=UTF-8");

		// 实际的逻辑是在这里
		PrintWriter out = response.getWriter();
		out.println("用户信息:\n<h1>" + userInfo + "</h1>");
		out.println("获取博主的勋章:\n<h1>" + medal + "</h1>");
		out.println("获取博主的专栏:\n<h1>" + column + "</h1>");
//		out.println("获取博主的文章列表:\n<h1>" + articlelist + "</h1>");
		out.println("发表/修改文章:\n<h1>" + savearticle + "</h1>");
		// out.println("<a href='" + urls + "'>get_access_token</a>");
	}

	public void destroy() {
		// 什么也不做
	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/
